import Promise from 'promise';
import { RepoModel } from '../models';

const fields = null;

export async function find(conditions, options) {
  try {
    const docs = await RepoModel.find(conditions).setOptions(options).exec();
    return Promise.resolve(docs);
  } catch (err) {
    logger.error(err);
    return Promise.reject(err);
  }
}

export function findOne(conditions, options) {
  const query = RepoModel.findOne(conditions).select(fields).setOptions(options);
  return query.exec();
}

export function update(id, data) {
  const query = RepoModel.findByIdAndUpdate(id, { $set: data });
  return query.exec();
}

export function remove(conditions) {
  return RepoModel.find(conditions).remove().exec();
}

export function create(cloneUrl) {
  const doc = new RepoModel();
  doc.cloneUrl = cloneUrl;
  return doc.save();
}
